<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>ToolTip Mouse Menu (requires XP/2k/NT) -- by Rajat</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>ToolTip Mouse Menu (requires XP/2k/NT) -- by Rajat</h1>

<p>This script displays a popup menu in response to briefly holding down
 the middle mouse button.  Select a menu item by left-clicking it.
 Cancel the menu by left-clicking outside of it.  A recent improvement
 is that the contents of the menu can change depending on which type of
 window is active (Notepad and Word are used as examples here).
</p>
<p><a href="TooltipMouseMenu.ahk">Download This Script</a> &nbsp;| &nbsp;<a href="index.htm">Other Sample Scripts</a> &nbsp;| &nbsp;<a href="../AutoHotkey.htm">Home</a></p>

<pre class="NoIndent"><em>; You can set any title here for the menu:</em>
MenuTitle = -=-=-=-=-=-=-=-

<em>; This is how long the mouse button must be held to cause the menu to appear:</em>
UMDelay = 20

SetFormat, float, 0.0
SetBatchLines, 10ms 
SetTitleMatchMode, 2
#SingleInstance


<em>;___________________________________________
;_____Menu Definitions______________________</em>

<em>; Create / Edit Menu Items here.
; You can't use spaces in keys/values/section names.</em>

<em>; Don't worry about the order, the menu will be sorted.</em>

MenuItems = Notepad/Calculator/Section 3/Section 4/Section 5


<em>;___________________________________________
;______Dynamic menuitems here_______________</em>

<em>; Syntax:
;     Dyn# = MenuItem|Window title</em>

Dyn1 = MS Word|- Microsoft Word
Dyn2 = Notepad II|- Notepad

<em>;___________________________________________</em>

Exit


<em>;___________________________________________
;_____Menu Sections_________________________</em>

<em>; Create / Edit Menu Sections here.</em>

Notepad:
Run, Notepad.exe
Return

Calculator:
Run, Calc
Return

Section3:
MsgBox, You selected 3
Return

Section4:
MsgBox, You selected 4
Return

Section5:
MsgBox, You selected 5
Return

MSWord:
msgbox, this is a dynamic entry (word)
Return

NotepadII:
msgbox, this is a dynamic entry (notepad)
Return


<em>;___________________________________________
;_____Hotkey Section________________________</em>

~MButton::
HowLong = 0
Loop
{
    HowLong ++
    Sleep, 10
    GetKeyState, MButton, MButton, P
    IfEqual, MButton, U, Break
}
IfLess, HowLong, %UMDelay%, Return


<em>;prepares dynamic menu</em>
DynMenu =
Loop
{
    IfEqual, Dyn%a_index%,, Break

    StringGetPos, ppos, dyn%a_index%, |
    StringLeft, item, dyn%a_index%, %ppos%
    ppos += 2
    StringMid, win, dyn%a_index%, %ppos%, 1000

    IfWinActive, %win%,
        DynMenu = %DynMenu%/%item%
}


<em>;Joins sorted main menu and dynamic menu</em>
Sort, MenuItems, D/
TempMenu = %MenuItems%%DynMenu%


<em>;clears earlier entries</em>
Loop
{
    IfEqual, MenuItem%a_index%,, Break
    MenuItem%a_index% =
}

<em>;creates new entries</em>
Loop, Parse, TempMenu, /
{
    MenuItem%a_index% = %a_loopfield%
}

<em>;creates the menu</em>
Menu = %MenuTitle%
Loop
{
    IfEqual, MenuItem%a_index%,, Break
    numItems ++
    StringTrimLeft, MenuText, MenuItem%a_index%, 0
    Menu = %Menu%`n%MenuText%
}

MouseGetPos, mX, mY
HotKey, ~LButton, MenuClick
HotKey, ~LButton, On
ToolTip, %Menu%, %mX%, %mY%
WinActivate, %MenuTitle%
Return


MenuClick:
HotKey, ~LButton, Off
IfWinNotActive, %MenuTitle%
{
    ToolTip
    Return
}

MouseGetPos, mX, mY
ToolTip
mY -= 3        <em>;space after which first line starts</em>
mY /= 13    <em>;space taken by each line</em>
IfLess, mY, 1, Return
IfGreater, mY, %numItems%, Return
StringTrimLeft, TargetSection, MenuItem%mY%, 0
StringReplace, TargetSection, TargetSection, %a_space%,, A
Gosub, %TargetSection%
Return
</pre>
</body>
</html>
